Page({
    data: {
      medicineInfo: null,
      isPlaying: false
    },
  
    // 添加一个状态锁
    isScanning: false,
  
    onLoad() {
      // 页面加载时启动扫码
      if (!this.isScanning) {
        this.startScan();
      }
    },
  
    onShow() {
      // 页面显示时，如果没有在扫码则启动扫码
      if (!this.isScanning) {
        this.startScan();
      }
    },
  
    startScan() {
      if (this.isScanning) return;
      
      this.isScanning = true;

      wx.scanCode({
        onlyFromCamera: true,
        scanType: ['qrCode', 'barCode'],
        success: (res) => {
          const code = res.result;
          wx.navigateTo({
            url: `/pages/medicine/medicine?code=${encodeURIComponent(code)}`
          });
        },
        fail: (err) => {
          if (err.errMsg !== 'scanCode:fail cancel') {
            wx.showToast({
              title: '扫码失败，请重试',
              icon: 'none'
            });
          }
        },
        complete: () => {
          this.isScanning = false;
        }
      });
    }
  })